package com.erp.gather.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.erp.gather.domain.bo.ErpProductUnifiedTakedownBo;
import com.erp.gather.domain.vo.ErpProductUnifiedTakedownVo;
import com.erp.gather.mapper.ErpCollectProductMapper;
import com.erp.gather.mapper.ErpSellerStoreProductMapper;
import com.erp.gather.service.IErpProductUnifiedTakedownService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.gather.api.domain.ErpCollectProduct;
import com.ruoyi.gather.api.domain.ErpProductUnifiedTakedown;
import com.ruoyi.gather.api.domain.ErpSellerStoreProduct;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * erp产品统一下架
 *
 * @author erp
 * @date 2023-06-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gather/productUnifiedTakedown")
public class ErpProductUnifiedTakedownController extends BaseController {

    private final IErpProductUnifiedTakedownService iErpProductUnifiedTakedownService;
    private final ErpSellerStoreProductMapper erpSellerStoreProductMapper;
    private final ErpCollectProductMapper erpCollectProductMapper;

    /**
     * 新增下架记录
     */
    @SaCheckPermission("gather:productUnifiedTakedown:setProductTakedown")
    @GetMapping("/setProductTakedown")
    public R<String> setProductTakedown(String takedownReason, Long collectProductId) {
        return iErpProductUnifiedTakedownService.setProductTakedown(takedownReason, collectProductId);
    }

    @SaCheckPermission("gather:productUnifiedTakedown:getProductUnifiedTakedownInfo")
    @PostMapping("/getProductUnifiedTakedownInfo")
    public R<Object> getProductUnifiedTakedownInfo(@RequestBody ErpProductUnifiedTakedown erpProductUnifiedTakedown) {
        return iErpProductUnifiedTakedownService.getProductUnifiedTakedownInfo(erpProductUnifiedTakedown);
    }

    @SaCheckPermission("gather:productUnifiedTakedown:getProductUnifiedTakedownListInfo")
    @PostMapping("/getProductUnifiedTakedownListInfo")
    public R<Object> getProductUnifiedTakedownListInfo(@RequestBody ErpProductUnifiedTakedown erpProductUnifiedTakedown) {
        return iErpProductUnifiedTakedownService.getProductUnifiedTakedownListInfo(erpProductUnifiedTakedown);
    }

    /**
     * 执行下架
     *
     * @param takedownId
     * @return
     */
    @SaCheckPermission("gather:productUnifiedTakedown:executeProductTakedown")
    @GetMapping("/executeProductTakedown")
    public R<String> executeProductTakedown(Long takedownId) {
        return iErpProductUnifiedTakedownService.executeProductTakedown(takedownId);
    }

    /**
     * 修复下架
     */
    @SaCheckPermission("gather:productUnifiedTakedown:repairProductTakedown")
    @GetMapping("/repairProductTakedown")
    public R<String> repairProductTakedown(Long collectProductId) {
        return iErpProductUnifiedTakedownService.repairProductTakedown(collectProductId);
    }


    /**
     * 查询erp产品统一下架列表
     */
    @SaCheckPermission("gather:productUnifiedTakedown:list")
    @GetMapping("/list")
    public TableDataInfo<ErpProductUnifiedTakedownVo> list(ErpProductUnifiedTakedownBo bo, PageQuery pageQuery) {
        return iErpProductUnifiedTakedownService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取erp产品统一下架详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("gather:productUnifiedTakedown:query")
    @GetMapping("/{id}")
    public R<ErpProductUnifiedTakedownVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable Long id) {
        return R.ok(iErpProductUnifiedTakedownService.queryById(id));
    }

    /**
     * 新增erp产品统一下架
     */
    @SaCheckPermission("gather:productUnifiedTakedown:add")
    @Log(title = "erp产品统一下架", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpProductUnifiedTakedownBo bo) {
        Long collectId = null;
        Long aeId = null;
        if (bo.getType() == 1) {
            ErpSellerStoreProduct product = erpSellerStoreProductMapper.selectOne(
                    new LambdaQueryWrapper<ErpSellerStoreProduct>().eq(ErpSellerStoreProduct::getAeProductId, bo.getAeProductId())
            );
            if (product == null) {
                return R.fail("该商品不存在");
            }
            collectId =  product.getCollectProductId();
            aeId =  product.getAeProductId();
        } else {
            ErpCollectProduct product = erpCollectProductMapper.selectOne(
                    new LambdaQueryWrapper<ErpCollectProduct>().eq(ErpCollectProduct::getId, bo.getAeProductId())
            );
            if (product == null) {
                return R.fail("该商品不存在");
            }
            collectId =  product.getId();
            aeId =  null;
        }
        bo.setCollectProductId(collectId);
        bo.setAeProductId(aeId);
        return toAjax(iErpProductUnifiedTakedownService.insertByBo(bo));
    }

    /**
     * 自动化侵权统一下架
     */
    @PostMapping("/autoUnifiedTakeDown")
    public R<String> autoUnifiedTakeDown(@RequestBody String jsonString) {
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        Map<String, Object> map = new HashMap<>(jsonObject);
        Long aeProductId = (Long) map.get("aeProductId");
        String violationNo = (String) map.get("violationNo");
        // 根据aeId找到采集表商品
        ErpSellerStoreProduct product = erpSellerStoreProductMapper.selectOne(
                new LambdaQueryWrapper<ErpSellerStoreProduct>().eq(ErpSellerStoreProduct::getAeProductId, aeProductId)
        );
        if (product == null) {
            return R.ok("该商品不存在");
        }
        Long collectProductId = product.getCollectProductId();
        ErpProductUnifiedTakedownBo bo = new ErpProductUnifiedTakedownBo();
        bo.setAeProductId(aeProductId);
        bo.setCollectProductId(collectProductId);
        bo.setType(1);
        bo.setIsEffective(0);
        bo.setIsExecute(0);
        bo.setTakedownReason("获取到违规信息，自动下架，违规编号：" + violationNo);
        Boolean aBoolean = iErpProductUnifiedTakedownService.insertByBo(bo);
        if (aBoolean) {
            return iErpProductUnifiedTakedownService.executeProductTakedown(bo.getId());
        }
        return R.fail();
    }


    @SaCheckPermission("gather:productUnifiedTakedown:addCollectProduct")
    @Log(title = "erp公共产品统一下架", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addCollectProduct")
    public R<Void> addCollectProduct(@Validated(AddGroup.class) @RequestBody ErpProductUnifiedTakedownBo bo) {
        ErpCollectProduct product = erpCollectProductMapper.selectOne(
                new LambdaQueryWrapper<ErpCollectProduct>().eq(ErpCollectProduct::getId, bo.getAeProductId())
        );
        if (product == null) {
            return R.fail("该商品不存在");
        }
        bo.setCollectProductId(product.getId());
        bo.setAeProductId(null);
        bo.setType(2);
        return toAjax(iErpProductUnifiedTakedownService.insertByBo(bo));
    }

    /**
     * 修改erp产品统一下架
     */
    @SaCheckPermission("gather:productUnifiedTakedown:edit")
    @Log(title = "erp产品统一下架", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpProductUnifiedTakedownBo bo) {
        return toAjax(iErpProductUnifiedTakedownService.updateByBo(bo));
    }

    /**
     * 删除erp产品统一下架
     *
     * @param ids 主键串
     */
    @SaCheckPermission("gather:productUnifiedTakedown:remove")
    @Log(title = "erp产品统一下架", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpProductUnifiedTakedownService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
